/* Release 3.10 */
/*-------------------------------------------------------------------------*
*                                                                          *
*  IOSTREAM.CPP - 2.1 stream class implementations.      		   *
*                                                                          *
*  COPYRIGHT (C) 1989..1992 Clarion Software Corporation.                  *
*  All Rights Reserved                                                     *
*                                                                          *
*--------------------------------------------------------------------------*/

#include <clib.h>
#include <fstream.hpp>
#include <iomanip.hpp>
#include <iostream.hpp>
#include <stdiostr.hpp>

extern "C" char *_ld_format(char *_fmt_buf, LONGDOUBLE num, int flag, int prec);
extern "C" char *_v_format(unsigned long, char *s);

#pragma module(init_prio=>22)

//**************************************************************************
//  ios implementation
//**************************************************************************

const long ios::basefield   = dec|oct|hex;
const long ios::adjustfield = left|right|internal;
const long ios::floatfield  = scientific|fixed;
void  (*ios::stdioflush)() = NULL;
long ios::nextbit = 10;
int  ios::usercount = 0;

long ios::flags(long f) {

  long t;

  t=x_flags;
  x_flags=f;
  return t;
}

long ios::setf(long sbits,long fld)
  {
  long t;

  t=x_flags&fld;
  x_flags&=~fld;
  sbits&=fld;
  x_flags|=sbits;
  return t;
  }

long ios::setf(long sbits) {

  long t;

  t=x_flags;
  x_flags|=sbits;
  return t;
}

long ios::unsetf(long sbits) {

  long t;

  t=x_flags;
  x_flags&=~sbits;
  return t;
}


ostream* ios::tie(ostream* ost) {

  ostream* t;

  t=x_tie;
  x_tie=ost;
  return t;
}

ios::ios(streambuf *sb) {

  init(sb);
}

void ios::init(streambuf *sb) {

  _bp=sb;
  x_tie=NULL;
  state=0;
  ispecial=skipping;
  ospecial=0;
  x_flags=skipws|right|scientific;
  x_precision=6;
  x_width=0;
  x_fill=' ';
  userwords=NULL;
  nwords=0;
  delbuf=0;
}

ios::ios() {

}

ios::~ios() {

}

int ios::skip(int sk) {

  int t;

  t=((x_flags&skipws) != 0);
  if(sk) {
    x_flags|=skipws;
    ispecial|=skipping;
  }
  else {
    x_flags&=~skipws;
    ispecial&= (~skipping);
  }
  return(t);
}

long ios::bitalloc() {

  if(nextbit >= 31)
    return 0;
  return 1<<++nextbit;
}

int ios::xalloc() {

  return usercount++;
}


_STATIC long err_ret=0;
_STATIC void * err_ret_ptr=NULL;

long& ios::iword(int n) {

  if((n >= 0) && (n < usercount)) {
    if(n >= nwords)
      uresize(n);
    if(userwords)
      return userwords[n].x;
  }
  setstate(failbit);
  return err_ret;
}


void* & ios::pword(int n) {

  if((n >= 0) && (n < usercount)) {
    if(n >= nwords)
      uresize(n);
    if(userwords)
      return userwords[n].p;
  }
  setstate(failbit);
  return err_ret_ptr;
}

void ios::uresize(int n) {

  nwords=n+1;
  userwords=(ios::ios_user_union *) realloc(userwords, nwords);
}


#ifndef _WINDOWS
_STATIC stdiobuf cin_sb (stdin);
_STATIC stdiobuf cout_sb(stdout);
_STATIC stdiobuf cerr_sb(stderr);
_STATIC stdiobuf clog_sb(stderr);


void ios::sync_with_stdio() {

  static done=0;

  if(!done) {
    done=1;
    cin=(streambuf *) &cin_sb;
    cout=(streambuf *) &cout_sb;
    cerr=(streambuf *) &cerr_sb;
    clog=(streambuf *) &clog_sb;
    cin.setf(ios::unitbuf|stdio);
    cout.setf(ios::unitbuf|stdio);
    clog.setf(ios::unitbuf|stdio);
  }
}
#endif

void ios::setstate(int st) {

  state|=st;
}

void ios::clear(int st) {

  state=st;
}

//**************************************************************************
//  streambuf implementation
//**************************************************************************


streambuf::streambuf() {

  char *b=new char[512];

  _base=NULL;
  setbuf(b, 512);
  _alloc=1;
}

streambuf::streambuf(char *s, int sz) {

  _base=NULL;
  setbuf(s, sz);
  _alloc=0;
}

streambuf::~streambuf() {

  if(_alloc && _base)
    delete _base;
}


streambuf * streambuf::setbuf(char *s, int sz, int pos) {

  if(_base)
    return NULL;
  setb(s, s+sz);
  if(s && sz) {
    setp(s+pos, s+sz/2);
    setg(s+sz/2, s+sz/2+pos, s+sz);
  }
  else {
    setp(NULL, NULL);
    setg(NULL, NULL, NULL);
  }
  return this;
}

streambuf* streambuf::setbuf(char *s, int sz) {

  if(_base)
    return NULL;
  setb(s, s+sz);
  if(s && sz) {
    setp(s, s+sz/2);
    setg(s+sz/2, s+sz, s+sz);
  }
  else {
    setp(NULL, NULL);
    setg(NULL, NULL, NULL);
  }
  return this;
}

int streambuf::sputn(const char *_s, int _n) {

  if(_n <= (_epptr - _pptr)) {
    memcpy(_pptr, _s, _n);
    pbump(_n);
    return _n;
  }
  return do_sputn(_s, _n);
}

int streambuf::do_sputn(const char *_s, int _n) {

  int t, nw;

  nw=0;
  while(nw < _n) {
    if(_pptr >= _epptr)
      overflow(_s[nw++]);
    t = _epptr - _pptr;
    if(t) {                   // special case for unbuffered
      memcpy(_pptr, &_s[nw], t);
      nw+=t;
      pbump(t);
    }
  }
  return nw;
}

int streambuf::overflow(int) {

  return EOF;
}

int streambuf::sgetn(char *_s, int _n) {

  if(_n <= (_egptr - _gptr)) {
    memcpy(_s, _gptr, _n);
    gbump(_n);
    return _n;
  }
  return do_sgetn(_s, _n);
}


int streambuf::do_sgetn(char *_s, int _n) {

  int t, nr;

  nr=0;
  while(nr < _n) {
    if(_gptr >= _egptr) {
      if(underflow() == EOF)
        break;
    }
    t = _egptr - _gptr;
    memcpy(&_s[nr], _gptr, t);
    nr+=t;
    gbump(t);
  }
  return nr;
}

int streambuf::underflow() {

  return EOF;
}

int streambuf::pbackfail(int) {

  return EOF;
}

streampos streambuf::seekoff(streamoff, ios::seek_dir, int) {

  return EOF;
}

streampos streambuf::seekpos(streamoff off, int mode) {

  streampos pos;

  if((pos=seekoff(off, ios::beg, mode)) == EOF)
    return EOF;
  return pos;
}

int streambuf::sync() {

  if(_gptr == _egptr)
    return 0;
  return EOF;
}

int streambuf::dbp() {

#ifndef _WINDOWS
  printf("base = %p, ebuf  = %p, pbase = %p\n", _base, _ebuf, _pbase);
  printf("pptr = %p, epptr = %p\n", _pptr, _epptr);
  printf("gptr = %p, egptr = %p, eback = %p\n", _gptr, _egptr, _eback);
#endif
  return 1;
}
void streambuf::setp(char *p, char *ep) {

  _pbase=p;
  _pptr=p;
  _epptr=ep;
}

void streambuf::setg(char *eb, char *g, char *eg) {

  _eback=eb;
  _gptr=g;
  _egptr=eg;
}

void streambuf::setb(char *b, char *eb, int i) {

  if(_base && _alloc)
    delete _base;
  _base=b;
  _ebuf=eb;
  _alloc=i;
  _unbuf=((!b) && (!eb));
}

int streambuf::doallocate() {

  char *b;

  b=new char[512];
  if(b == NULL)
    return EOF;
  _alloc=1;
  setbuf(b, 512);
  return 1;
}

int streambuf::do_snextc() {

  int na;

  na=_gptr-_egptr;
  underflow();
  if(na > 0)
    _gptr+=na;
  return *_gptr;
}

//**************************************************************************
//  istream implementation
//**************************************************************************

istream::istream(streambuf *sb) {

  ios::init(sb);
  _gcount=0;
}

istream::~istream() {
  if(delbuf)
    delete _bp;
}

istream::istream() {

  _gcount=0;
}

istream::istream(streambuf *sb, int sk, ostream* tie) {

  ios::init(sb);
  x_tie=tie;
  skip(sk);
  _gcount=0;
}


istream::istream(int sz, char* b, int sk) : ios() {

  streambuf *p;

  p=new streambuf(b, sz);
  ios::init(p);
  skip(sk);
  _gcount=0;
  delbuf=1;
}

istream::istream(int fd, int sk, ostream* tie) : ios() {

  filebuf *f;

  f=new filebuf(fd);
  ios::init(f);
  x_tie=tie;
  skip(sk);
  _gcount=0;
  delbuf=1;
}

istream& istream::seekg(streampos p) {

  state &= ~(failbit|eofbit);
  if(state == goodbit)
    _bp->seekoff(p, ios::beg, ios::in);
  return *this;
}

istream& istream::seekg(streamoff p, ios::seek_dir d) {

  state &= ~(failbit|eofbit);
  if(state == goodbit)
    _bp->seekoff(p, d, ios::in);
  return *this;
}

streampos istream::tellg() {

  if(state == goodbit)
    return _bp->seekoff(0L, ios::cur, ios::in);
  return (streampos) -1;
}

istream& istream::ignore(int n, int delim) {

  int c, cnt;

  cnt=0;
  while(cnt++ < n) {
    if(((c=_bp->sbumpc()) == delim) && (delim != EOF)) {
      _bp->sputbackc(c);
      break;
    }
    if(c == EOF) {
      setstate(failbit|eofbit);
      break;
    }
  }
  return *this;
}

istream& istream::read(signed char* s, int n) {

  int cnt, c;

  _gcount=0;
  if(state == goodbit) {
    cnt=0;
    while(cnt < n) {
      c=_bp->sbumpc();
      if(c == EOF) {
        setstate(eofbit|failbit);
        break;
      }
      s[cnt++]=c;
      ++_gcount;
    }
  }
  return *this;
}

istream& istream::putback(char c) {

  if(state == goodbit) {
    if(_bp->sputbackc(c) == EOF)
      setstate(eofbit|failbit);
  }
  return *this;
}

istream& istream::getline(signed char* s, int n, char t) {

  int c, p=0;

  if(ipfx1()) {
    while(p < n-1) {
      c=_bp->sbumpc();
      if(c == t) {
        break;
      }
      if(c == EOF) {
        setstate(eofbit);
        if(p == 0)
          setstate(failbit);
        break;
      }
      s[p++]=c;
    }
  }
  s[p]='\0';
  return *this;
}

istream& istream::operator>>(signed char *s) {

  int lim, c, p=0;

  if(x_width) {
    lim=x_width-1;
    x_width=0;
  }
  else
    lim = INT_MAX;
  _gcount=0;
  if(ipfx0()) {
    while(p < lim) {
      c=_bp->sbumpc();
      if(c == EOF) {
        setstate(failbit|eofbit);
        break;
      }
      if(isspace(c)) {
        _bp->sputbackc(c);
        break;
      }
      s[p++]=c;
      ++_gcount;
    }
  }
  s[p]='\0';
  return *this;
}

istream& istream::get(signed char *s, int len, char delim) {

  int c, p=0;

  _gcount=0;
  if(ipfx1()) {
    while(p < len-1) {
      c=_bp->sbumpc();
      if(c == delim) {
        _bp->sputbackc(c);
        break;
      }
      if(c == EOF) {
        setstate(eofbit);
        if(p == 0)
          setstate(failbit);
        break;
      }
      s[p++]=c;
      ++_gcount;
    }
  }
  s[p]='\0';
  return *this;
}

istream& istream::get(unsigned char& _c) {

  if( ipfx1() ) {
    if( _bp->in_avail() ) {
      _gcount = 1;
      _c = _bp->sbumpc();
    }
    else
      _c = do_get();
  }
  return *this;
}

istream& istream::get(signed char& _c) {

  if( ipfx1() ) {
    if( _bp->in_avail()) {
      _gcount = 1;
      _c = _bp->sbumpc();
    }
    else
      _c = do_get();
  }
  return *this;
}

istream& istream::get(     char& _c) {

  if( ipfx1() ) {
    if( _bp->in_avail()) {
      _gcount = 1;
      _c = _bp->sbumpc();
    }
    else
      _c = do_get();
  }
  return *this;
}


int istream::get() {

  if( ipfx1() ) {
    int _c = _bp->sbumpc();
    if( _c == EOF )
      setstate(eofbit);
    else
      _gcount = 1;
    return _c;
  }
  return EOF;
}


istream& istream::get(streambuf& s, char t) {

  int c, lim;

  if(x_width) {
    lim=x_width;
    x_width=0;
  }
  else
    lim=INT_MAX;
  _gcount=0;
  if(state == goodbit) {
    while(lim--) {
      c=_bp->sbumpc();
      if(c == EOF) {
        setstate(eofbit);
        break;
      }
      if(c == t) {
        _bp->sputbackc(c);
        break;
      }
      if(s.sputc(c) == EOF) {
        setstate(failbit);
        break;
      }
      ++_gcount;
    }
  }
  return *this;
}

istream& istream::operator>>(ios& (*f)(ios &)) {

  f(*this);                // NB !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  return *this;
}

_STATIC long double getreal(istream& is, int& pass) {

  int n=0;
  char c;
  char _fmt_buf[64];
  int good_scan=0;

  is >> c;
  while(is.good())
  {
    if((c == '+') || (c == '-')) {	   /* leading sign */
      good_scan=1;
      _fmt_buf[n++]= (char) c;
      c= is.get();
      if(!is.good())
        break;
    }
    for(int q=0; q<2; q++) {
      while(isdigit(c)) {     /* get digits */
        good_scan=1;
        _fmt_buf[n++]= (char) c;
        c= is.get();
        if(!is.good())
          break;
      }
      if((c == DEC_POINT) && (q == 0)) {   /* decimal point */
        _fmt_buf[n++]= (char) c;
        c= is.get();
        if(!is.good())
          break;
      }
      else if(q == 0)
        break;
    }                  /* go back for more digits if first iteration */
    if((c == 'E') || (c == 'D') || (c == 'e') || (c == 'd')) {
      _fmt_buf[n++]= (char) c;
      c= is.get();
      if(!is.good())
        break;
    }
    else
      break;
    if((c == '+') || (c == '-')) {
      _fmt_buf[n++]= (char) c;
      c= is.get();
      if(!is.good())
        break;
    }                   /* exponent digits */
    while(isdigit(c)) {
      good_scan=1;
      _fmt_buf[n++]= (char) c;
      c= is.get();
      if(!is.good())
        break;
    }
    break;
  }
  _fmt_buf[n]=0;           /* terminate string */
  if(is.good())
    is.putback(c);
  pass=good_scan;
  return atofl(_fmt_buf);	 /* convert and assign if not suppressed */
}

_STATIC long getlong(istream& is, int us, int& pass) {

  int negative=0, flag=0;
  long temp=0;
  int good_scan=0;
  char c;

  is >> c;
  if(!is.good())
    goto error;
  if(c == '+') {
    good_scan=1;
    c= is.get();
    if(!is.good())
      goto error;
  }
  else if(c == '-') {                  /* number is negative */
    good_scan=1;
    negative=1;
    c= is.get();
    if(!is.good())
      goto error;
    else if(us)
      return(0);
  }
  if(c =='0') {
    good_scan=1;
    flag=_OCT;
    c= is.get();
    if(!is.good())
      goto error;
    c=tolower(c);
    if(c == 'x') {
      flag=_HEX;
      c= is.get();
      if(!is.good())
        goto error;
    }
  }
  if(is.flags()&ios::dec) {
    if(flag == _HEX)
      c='x';
    flag=0;
  }
  switch(flag) {
    case _HEX :
      while(isxdigit(c=tolower(c))) {
        temp*=16;
        if(!isdigit(c))
          c-=87;
        else
          c-='0';
        temp+=c;
        c= is.get();
        if(!is.good())
          goto error;
      }
      break;
    case _OCT :
      while((c >= '0') && (c <= '7')) {
        temp*=8;
        temp+=c-'0';
        c= is.get();
        if(!is.good())
          goto error;
      }
      break;
    default :
      while(isdigit(c)) {
        good_scan=1;
        temp*=10;
        temp+=c-'0';
        c= is.get();
        if(!is.good())
          goto error;
      }
      break;
  }
  c=tolower(c);
  if((c != 'u') && (c != 'l'))
    is.putback(c);
error:
  if(negative)
    return -temp;
  pass=good_scan;
  return temp;
}

istream& istream::operator>>(short& s) {

  short t;
  int pass;

  t=getlong(*this, 0, pass);
  if(pass)
    s=(short) t;
  else
    setstate(failbit);
  return *this;
}

istream& istream::operator>>(int& i) {

  int t;
  int pass;

  t=(int)getlong(*this, 0, pass);
  if(pass)
    i=(int) t;
  else
    setstate(failbit);
  return *this;
}

istream& istream::operator>>(long& l) {

  long t;
  int pass;

  t=getlong(*this, 0, pass);
  if(pass)
    l= t;
  else
    setstate(failbit);
  return *this;
}

istream& istream::operator>>(unsigned short& s) {

  unsigned short t;
  int pass;

  t=getlong(*this, 1, pass);
  if(pass)
    s=(unsigned short) t;
  else
    setstate(failbit);
  return *this;
}

istream& istream::operator>>(unsigned int& i) {

  unsigned int t;
  int pass;

  t=(unsigned)getlong(*this, 1, pass);
  if(pass)
    i=(unsigned int) t;
  else
    setstate(failbit);
  return *this;
}

istream& istream::operator>>(unsigned long& l) {

  unsigned long t;
  int pass;

  t=getlong(*this, 1, pass);
  if(pass)
    l= (unsigned long) t;
  else
    setstate(failbit);
  return *this;
}

istream& istream::operator>>(long double& ld) {

  long double t;
  int pass;

  t=getreal(*this, pass);
  if(pass)
    ld= t;
  else
    setstate(failbit);
  return *this;
}

istream& istream::operator>>(double& d) {

  long double t;
  int pass;

  t=getreal(*this, pass);
  if(pass)
    d= (double) t;
  else
    setstate(failbit);
  return *this;
}

istream& istream::operator>>(float& f) {

  long double t;
  int pass;

  t=getreal(*this, pass);
  if(pass)
    f= (float) t;
  else
    setstate(failbit);
  return *this;
}

istream& istream::operator>>(streambuf* sb) {


  int c;

  if(state == goodbit) {
    while(1) {
      if(!ipfx0())
        break;
      c=_bp->sbumpc();
      if(c == EOF) {
        setstate(eofbit);
        break;
      }
      if(sb->sputc(c) == EOF) {
        putback(c);
        break;
      }
      ++_gcount;
    }
  }
  return *this;
}

int istream::do_ipfx(int need) {

  if(state != goodbit)
    return 0;
  if((!need) || (need > _bp->in_avail())) {   // skip and flush tie
    if(x_tie)
      x_tie->flush();
    if(ispecial&skipping)
      eatwhite();
  }
  return (state == goodbit);
}

void istream::eatwhite() {

  int c;

  while(isspace(c=_bp->sgetc())) {
    _bp->stossc();
  }
  if(c == EOF)
    setstate(failbit|eofbit);
}


int istream::do_get() {

 int t;

 if ((t=_bp->underflow()) == EOF)
   setstate(eofbit);
 else {
   _bp->sbumpc();
   return t;
 }
}

//**************************************************************************
//  ostream implementation
//**************************************************************************

ostream::ostream(streambuf *sb)  {

  ios::init(sb);
  ospecial=translate;
}

ostream::~ostream() {
  if(delbuf)
    delete _bp;
}

ostream::ostream() {

  ospecial=translate;
}


ostream::ostream(int sz, char* b) {

  streambuf *p;

  p=new streambuf(b, sz);
  ios::init(p);
  delbuf=1;
  ospecial=translate;
}

ostream::ostream(int fd) {

  filebuf *f;

  f=new filebuf(fd);
  ios::init(f);
  delbuf=1;
  ospecial=translate;
}

ostream& ostream::seekp(streampos p) {

  state &= ~(failbit|eofbit);
  if(state == goodbit)
    _bp->seekoff(p, ios::beg, ios::out);
  return *this;
}

ostream& ostream::seekp(streamoff p, ios::seek_dir d) {

  if(state == goodbit)
    _bp->seekoff(p, d, ios::out);
  return *this;
}

streampos ostream::tellp() {

  if(state == goodbit)
    return _bp->seekoff(0L, ios::cur, ios::out);
  return (streampos) -1;
}

ostream& ostream::write(const char*s, int n) {

  int cnt;

  if(state == goodbit) {
    if(ospecial) {
      cnt=0;
      while(cnt < n) {
        if(_bp->sputc((unsigned char) s[cnt]) == EOF) {
          setstate(failbit);
          break;
        }
        ++cnt;
      }
    }
    else {
      if(_bp->sputn(s, n) != n)
        setstate(failbit);
    }
  }
  return *this;
}


_STATIC int pad_output(ostream& os, int l) {
  int pw;

  pw=os.width();
  while(pw > l) {
    if(os.rdbuf()->sputc(os.fill()) == EOF) {
      return EOF;
    }
    --pw;
  }
  os.width(0);
  return 0;
}


ostream& ostream::operator<<(signed char c) {

  if(opfx()) {
    if(!(x_flags&left)) {
      if(pad_output(*this, 1) == EOF) {
        setstate(failbit);
        return *this;
      }
    }
    if(state == goodbit) {
      if(_bp->sputc(c) == EOF)
        setstate(failbit);
      else {
        if(x_flags&left) {
          if(pad_output(*this, 1) == EOF)
            setstate(failbit);
            return *this;
        }
      }
    }
    osfx();
  }
  return *this;
}

void ostream::outlong(unsigned long l, int issigned) {

  int radix, np, len, signchar;
  char fmt_buf[64];
  int c;

  if(x_flags&hex)
    radix=16;
  else if(x_flags&oct)
    radix=8;
  else
    radix=10;
  np=3;
  if(issigned)
    ltoa(l, &fmt_buf[np], radix);
  else
    ltoa(l, &fmt_buf[np], radix);

  if(x_flags&showbase) {
    if(x_flags&hex)
      fmt_buf[--np]='x';
    if(x_flags&(hex|oct))
      fmt_buf[--np]='0';
  }
  if(x_flags&uppercase)
    strupr(&fmt_buf[np]);
  else
    strlwr(&fmt_buf[np]);
  if(((x_flags&showpos) && (!(x_flags&(hex|oct)))) || (l < 0)) {
    if(l >= 0)
      fmt_buf[--np]= '+';
    else
      fmt_buf[--np]= ' ';
    signchar=1;
  }
  else
    signchar=0;
  len=strlen(&fmt_buf[np]);

  if((x_flags&right) || (!(x_flags&adjustfield))) {
    if(pad_output(*this, len) == EOF) {
      setstate(failbit);
      return;
    }
  }
  if(x_flags&internal) {
    int dec=0;
    if(signchar || (x_flags&showbase)) {
      c=_bp->sputc(fmt_buf[np++]);
      ++dec;
      if((x_flags&hex) && (x_flags&showbase)) {
        c=_bp->sputc(fmt_buf[np++]);
        ++dec;
      }
      if(c == EOF) {
        setstate(failbit);
        return;
      }
    }
    if(pad_output(*this, len) == EOF) {
      setstate(failbit);
      return;
    }
    len-=dec;
  }
  int n=0;
  while(n++ < len) {
    if(_bp->sputc(fmt_buf[np++]) == EOF) {
      setstate(failbit);
      return;
    }
  }
  if(x_flags&left) {
    if(pad_output(*this, len) == EOF)
      setstate(failbit);
  }
  return;
}


ostream& ostream::operator<<(long l) {

  if(opfx()) {
    outlong((unsigned long) l, 1);
    osfx();
  }
  return *this;
}

ostream& ostream::operator<<(unsigned long l) {

  if(opfx()) {
    outlong(l, 0);
    osfx();
  }
  return *this;
}

ostream& ostream::operator<<(long double ld) {

  unsigned flag, signchar, len;
  char buf[64];
  char *res;

  if(opfx()) {
    if(x_flags&scientific)
      flag=(PF_EXP|PF_CPP);
    else if(x_flags&fixed)
      flag=PF_CPP;
    else
      flag=(PF_GTYPE|PF_CPP);
    if(x_flags&showpoint)
      flag|=PF_HASH;
    if(x_flags&showpos)
      flag|=PF_SIGN;

    res=_ld_format(buf, ld, flag, x_precision);

    signchar=((res[0] == '+') || (res[0] == '-'));
    len=strlen(res);

    int p=0;
    if(x_flags&right) {
      if(pad_output(*this, len) == EOF) {
        setstate(failbit);
        return *this;
      }
    }
    if((x_flags&internal) && (signchar)){
      if(_bp->sputc(res[p++]) == EOF) {
        setstate(failbit);
        return *this;
      }
      if(pad_output(*this, len) == EOF) {
        setstate(failbit);
        return *this;
      }
      --len;
    }
    int n=0;
    while(n++ < len) {
      if(_bp->sputc(res[p++]) == EOF) {
        setstate(failbit);
        return *this;
      }
    }
    if(x_flags&left) {
      if(pad_output(*this, len) == EOF) {
        setstate(failbit);
        return *this;
      }
    }
    osfx();
  }
  return *this;
}

void  ostream::outstr(const char *s) {

  int len, p=0;

  len=strlen(s);
  if((x_width) && (len > x_width))
    len=x_width;
  if(!(x_flags&left)) {
    if(pad_output(*this, len) == EOF) {
      setstate(failbit);
      return;
    }
  }
  while(p < len) {
    if(_bp->sputc(s[p++]) == EOF) {
      setstate(badbit);
      return;
    }
  }
  if(x_flags&left) {
    if(pad_output(*this, len) == EOF) {
      setstate(failbit);
      return;
    }
  }
  x_width=0;
}

ostream& ostream::operator<<(void *p) {

  char buf[12];
  char *ob;

  if(opfx()) {
    ob=&buf[2];
    _v_format((unsigned long) p, ob);
    if(x_flags&ios::showbase) {
      buf[0]='0';
      buf[0]='x';
      ob=buf;
    }
    outstr(ob);
    osfx();
  }
  return *this;
}

ostream& ostream::operator<<(streambuf *sb) {

  int c, lim;

  if(opfx()) {
    if(x_width) {
      lim=x_width;
      x_width=0;
    }
    else
      lim=INT_MAX;
    while((lim--) && ((c=sb->sbumpc()) != EOF)) {
      if(_bp->sputc(c) == EOF) {
        setstate(failbit);
        break;
      }
    }
    osfx();
  }
  return *this;
}

ostream& ostream::operator<<(ios& (*f)(ios &i)) {

  if(opfx()) {
    f(*this);
    osfx();
  }
  return *this;
}

ostream& ostream::operator<<(const char *s) {

  if(opfx()) {
    outstr(s);
    osfx();
  }
  return *this;
}


int ostream::do_opfx() {

  if(state != goodbit)
    return 0;
  if(x_tie)
    x_tie->flush();
  return 1;
}


int ostream::do_osfx() {

  if(x_flags&unitbuf)
    flush();
#ifndef _WINDOWS
  if(x_flags&stdio) {
    ::fflush(stdin);
    ::fflush(stdout);
  }
#endif
  return good();
}

ostream& ostream::flush() {

  if(_bp->sync() == EOF)
    setstate(failbit);
  return(*this);
}

//**************************************************************************
//  iostream implementation
//**************************************************************************


iostream::iostream(streambuf *sb) : istream(sb), ostream(sb) {

}

iostream::iostream() {

}

iostream::~iostream() {

}

//**************************************************************************
//  withassign stream implementation
//**************************************************************************

ostream_withassign::ostream_withassign() {

  assign_const=_assign_init_magic;
}

ostream_withassign::~ostream_withassign() {

}

istream_withassign::istream_withassign() {

  assign_const=_assign_init_magic;
}

istream_withassign::~istream_withassign() {

}

iostream_withassign::iostream_withassign() {

  assign_const=_assign_init_magic;
}

iostream_withassign::~iostream_withassign() {

}

istream_withassign& istream_withassign::operator=(istream& is) {

  ios::init(is.rdbuf());
  x_tie=is.tie();
  state=is.rdstate();
  x_flags=is.flags();
  x_precision=is.precision();
  x_width=is.width();
  x_fill=is.fill();
  return *this;
}

istream_withassign& istream_withassign::operator=(streambuf* sb) {

  ios::init(sb);
  return *this;
}

ostream_withassign& ostream_withassign::operator=(ostream& os) {

  ios::init(os.rdbuf());
  x_tie=os.tie();
  state=os.rdstate();
  x_flags=os.flags();
  x_precision=os.precision();
  x_width=os.width();
  x_fill=os.fill();
  return *this;
}

ostream_withassign& ostream_withassign::operator=(streambuf* sb) {

  ios::init(sb);
  return *this;
}

iostream_withassign& iostream_withassign::operator=(ios& iost) {

  ios::init(iost.rdbuf());
  x_tie=iost.tie();
  state=iost.rdstate();
  x_flags=iost.flags();
  x_precision=iost.precision();
  x_width=iost.width();
  x_fill=iost.fill();
  return *this;
}

iostream_withassign& iostream_withassign::operator=(streambuf* sb) {

  ios::init(sb);
  return *this;
}
ostream& endl(ostream& os) {

  os << "\n";
  return os;
}

ostream& ends(ostream& os) {

  os.put('\0');
  return os;
}

ostream& flush(ostream& os) {

  os.flush();
  return os;
}

ios& dec(ios& i) {

  i.setf(ios::dec, ios::basefield);
  return i;
}

ios& hex(ios& i) {

  i.setf(ios::hex, ios::basefield);
  return i;
}

ios& oct(ios& i) {

  i.setf(ios::oct, ios::basefield);
  return i;
}

istream& ws(istream& is) {

  int c;

  while(isspace(c=is.get()));
  if(c != EOF)
    is.rdbuf()->sputbackc(c);
  return is;
}

_STATIC char _cin_buf[BUFSIZ];
_STATIC char _cout_buf[BUFSIZ];
_STATIC char _clog_buf[BUFSIZ];


_STATIC filebuf cin_fb (0, _cin_buf,  BUFSIZ);
_STATIC filebuf cout_fb(1, _cout_buf, BUFSIZ);
_STATIC filebuf cerr_fb(2, NULL, 0);
_STATIC filebuf clog_fb(2, _clog_buf, BUFSIZ);

extern "C" void _init_standard() {

  cin=(streambuf *) &cin_fb;
  cout=(streambuf *) &cout_fb;
  cout.setf(ios::unitbuf);
  cerr=(streambuf *) &cerr_fb;
  clog=(streambuf *) &clog_fb;
  clog.setf(ios::unitbuf);
}

static ios& set_width(ios& iosc, int wid) {

  iosc.width(wid);
  return iosc;
}


smanip_int  setw(int _n) {

  return smanip_int(set_width, _n);
}

static ios& set_base(ios& iosc, int bas) {

  iosc.setf(bas, ios::basefield);
  return iosc;
}


smanip_int  setbase(int _b) {

  return smanip_int(set_base, _b);
}

static ios& reset_flags(ios& iosc, long f) {

  iosc.unsetf(f);
  return iosc;
}


smanip_long resetiosflags(long _f) {

  return smanip_long(reset_flags, _f);
}

static ios& set_flags(ios& iosc, long f) {

  iosc.flags(f);
  return iosc;
}

smanip_long setiosflags(long _f) {

  return smanip_long(set_flags, _f);
}

static ios& set_fill(ios& iosc, int fl) {

  iosc.fill(fl);
  return iosc;
}


smanip_int  setfill(int _f) {

  return smanip_int(set_fill, _f);
}

static ios& set_precision(ios& iosc, int pr) {

  iosc.precision(pr);
  return iosc;
}


smanip_int  setprecision(int _p) {

  return smanip_int(set_precision, _p);
}


istream_withassign cin;
ostream_withassign cout;
ostream_withassign cerr;
ostream_withassign clog;

int genericerror(int code, char *m) {

  cerr << m;
  exit(code);
  return 0;
}

